package com.sdy.kzyy.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sdy.kzyy.biz.model.StoreInventory;
import com.sdy.kzyy.biz.mapper.StoreInventoryMapper;
import com.sdy.kzyy.biz.service.KeyValueConfigurationsService;
import com.sdy.kzyy.biz.service.StoreInventoryService;
import com.sdy.mvc.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzq
 * @since 2020-03-04
 */
@Slf4j
@Service
public class StoreInventoryServiceImpl extends BaseServiceImpl<StoreInventory> implements StoreInventoryService {
    @Autowired
    private StoreInventoryMapper storeInventoryMapper;
    @Autowired
    private KeyValueConfigurationsService keyValueConfigurationsService;
    @Autowired
    private StoreInventoryService storeInventoryService;

    @Override
    public StoreInventory getStoreDate(String code) {
        QueryWrapper<StoreInventory> wrapper = new QueryWrapper<>();
        wrapper.eq("store_code", code);
        return list(wrapper).get(0);
    }

    @Override
    public boolean updateStoreKzCount(String storeCode) {
        return storeInventoryMapper.updateStoreKzCount(storeCode);
    }

    @Override
    public boolean updateStoreOnceCount() {
        Integer count = Integer.parseInt(keyValueConfigurationsService.getLimitationCount());
        StoreInventory storeInventory = new StoreInventory();
        storeInventory.setOnceCount(count);
        return update(storeInventory, new UpdateWrapper<StoreInventory>());
    }

    @Override
    public Boolean updateAllCount() {
        List<StoreInventory> listSi = storeInventoryService.list();
        Date date = new Date();
            for (StoreInventory list : listSi) {
                Integer kzCount = list.getKzCount() == null ? 0 : list.getKzCount();
                Integer replenishNextDay = list.getReplenishNextDay() == null ? 0: list.getReplenishNextDay();
                int allCount = kzCount + replenishNextDay;
                allCount = allCount < 0 ? 0 : allCount;
                StoreInventory storeInventory = list.setKzCount(allCount).setAllCount(allCount).setReplenishNextDay(0).setUpdateTime(date);
                boolean update = storeInventoryService.updateById(storeInventory);
                if (update != true) {
                    log.info("id为"+ list.getId() +"的store_inventory表数据自动补货出错!");
                }
            }
        return true;
    }

}
